﻿using lib;
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.Security.Cryptography.X509Certificates;
using System.Threading;

namespace Server
{
    class Server
    {
        private TcpListener listener;
        private string wachtwoord = "test", inlognaam = "test";
        private int maxaantalgebruikers = 2;
        private List<Client> clients;
        private Docclient docClient;
        private static X509Certificate serverCertificate = null;

        public static X509Certificate getCertificate()
        {
            return (serverCertificate == null) ? serverCertificate = X509Certificate.CreateFromCertFile("../../Server_TemporaryKey.pfx") : serverCertificate;
        }
        
        public Server() 
        {
            clients = new List<Client>();
            listener = new TcpListener(IPAddress.Parse("0.0.0.0"), 60530); // Port = 60530
            startLoop();
        }

        public List<Client> getClients()
        {
            return clients;
        }

        public Docclient getDocClient()
        {
            lock (docClient)
            {
                return docClient;
            }
        }

        public void startLoop()
        {
            listener.Start();
            while (true)
            {
                Console.WriteLine("Waiting for connection");
                TcpClient client = listener.AcceptTcpClient();
                SecureConnectionServer sc = new SecureConnectionServer(client, Server.getCertificate());
                String[] split = new String[3];
                split = sc.ReadObjectAsString().Split(':');
                Console.WriteLine(split[0]);
                if (split[0] == "Arts")
                {
                    // Create the thread for the DocConnection
                    if (split[1] == inlognaam && split[2] == wachtwoord)
                    {
                        sc.SendObject("connection accepted");
                        docClient = new Docclient(this, sc);
                        Thread docThread = new Thread(docClient.Handle);
                        docThread.Start(new DocClientHandler(client));
                        ClientHandler.tcpDoc = client;
                        Console.WriteLine("DocClient connected: {0}", (client.Client.RemoteEndPoint as IPEndPoint).Address.ToString());
                    }
                    else
                    {
                        sc.SendObject("connection refused");
                    }
                }
                else if (split[0] == "Fiets")
                {
                    // Start the thread.
                    Console.WriteLine("Client connected: {0}", (client.Client.RemoteEndPoint as IPEndPoint).Address.ToString());
                    Client newClient = new Client(this, sc);
                    clients.Add(newClient);
                    Thread clientThread = new Thread(newClient.Handle);
                    clientThread.Start(new ClientHandler(split[2], split[1], split[3], split[4], split[5]));
                    Console.WriteLine("new patient " + split[2] + " nummer: " + split[1] + " Gender: " + split[3] + " Age: " + split[4] + " Gewicht: " + split[5]);
                }
            }
        }
    }
}
